// pages/user/erji//videoMember/videoMember.js
var base = require('../../../../base.js');  //路径可能做相应调整
const _ = base._; //翻译函数
const PAGES = require('../../../../utils/pages.js')

const { HTTP } = require('../../../../lib/http.js')
const { URL } = require('../../../../lib/urls.js')
const { getUserToken, getUserInfo, getNetUserInfo } = require('../../../../lib/user.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _t: [],
    userLeve: "",
    userMemberId: "",
    daoqiTime: "",
    dataList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      _t: base._t(), //翻译
    });
    this.setData({
      userLeve: "VIP:" + getUserInfo().MemberLevel,
      userMemberId: getUserInfo().userId,
      daoqiTime: getUserInfo().VideoEndTime ? getUserInfo().HasVideoMember : _("暂未开通")
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getNetPiceList();
  },

  //获取价格列表
  getNetPiceList: function () {
    let that = this;
    HTTP({
      url: URL.mamber.videoMemberPice,
      userToken: getUserToken(),
      data: {
      },
      success: (ress) => {
        // MonthPrice = 1,
        //   TwoMonthPrice = 2,
        //   ThreeMonthPrice = 3,
        //   HalfYearPrice = 6,
        //   YearPrice = 12,
        //   TwoYearPrice = 24
        var res = ress.Data;
        console.log("hlw:" + res)
        var list = []
        if (res.MonthPrice!=null&&res.MonthPrice>0){
          var model = { time: "一个月", pice: res.MonthPrice,level:1};
          list.push(model);
        }
        if (res.TwoMonthPrice !=null&&res.TwoMonthPrice > 0) {
          var model = { time: "两个月", pice: res.TwoMonthPrice, level: 2 };
          list.push(model);
        }
        if (res.ThreeMonthPrice != null&&res.ThreeMonthPrice > 0) {
          var model = { time: "三个月", pice: res.ThreeMonthPrice, level: 3 };
          list.push(model);
        }
        if (res.HalfYearPrice != null&&res.HalfYearPrice > 0) {
          var model = { time: "半年", pice: res.HalfYearPrice, level: 6 };
          list.push(model);
        }
        if (res.YearPrice != null&&res.YearPrice > 0) {
          var model = { time: "一年", pice: res.YearPrice, level: 12 };
          list.push(model);
        }
        if (res.TwoYearPrice != null&&res.TwoYearPrice > 0) {
          var model = { time: "两年", pice: res.TwoYearPrice, level: 24 };
          list.push(model);
        }
        that.setData({
          dataList: list,
        });
      },
      fail: (res) => {
        console.log(res)
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },

  //购买课程
  onItemTap: function (e) {
    var data = e.currentTarget.dataset.level;
    this.netSubmit(data);
  },

  //购买
  // MonthPrice = 1,
        //   TwoMonthPrice = 2,
        //   ThreeMonthPrice = 3,
        //   HalfYearPrice = 6,
        //   YearPrice = 12,
        //   TwoYearPrice = 24
  netSubmit: function (level) {
    let that = this;
    HTTP({
      url: URL.mamber.videoMemberPay,
      userToken: getUserToken(),
      data: {
        VideoMemberLevel:level
      },
      success: (ress) => {
        var res = ress.Data;
        //支付开始
        wx.requestPayment({
          timeStamp: res.timeStamp,
          nonceStr: res.nonceStr,
          package: res.package,
          signType: res.signType,
          paySign: res.paySign,
          success: function (res) {
            //支付成功
            wx.showToast({
              title: _('支付成功'),
              icon: 'succes',
              duration: 1000,
              mask: true
            })
            setTimeout(function () {
              //你需要执行的代码
              wx.navigateBack({
                delta: 1
              })
            }, 1000)
          },
          fail: function (res) {
            console.log("hlwfail:" + res.data)
            wx.showToast({
              title: _('支付失败'),
              icon: 'none'
            })
          }
        })
        //支付结束
      },
      fail: (res) => {
        console.log(res)
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },
})